home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / kpathsea / putenv.c < prev    next >
C/C++ Source or Header  |  1996-11-02  |  3KB  |  125 lines

  1. /****************************************************************/
  2. /*                                */
  3. /*    putenv(3)                        */
  4. /*                                */
  5. /*        Change or add an environment entry        */
  6. /*                                */
  7. /****************************************************************/
  8. /*   origination        1987-Oct-7               T. Holm    */
  9. /* (slightly modified by karl@cs.umb.edu for kpathsea.)         */
  10. /****************************************************************/
  11.  
  12. /* Modified by Klaus Gebhardt, 1995 */
  13.  
  14. /* for HAVE_PUTENV and const -- need nothing else.  */
  15. #include <kpathsea/c-auto.h>
  16.  
  17. #ifndef HAVE_PUTENV /* whole file */
  18.  
  19. #if defined (__EMX__)
  20. int strncmp();
  21. #endif
  22.  
  23. /*
  24. Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
  25. From: tholm@uvicctr.UUCP (Terrence W. Holm)
  26. Newsgroups: comp.os.minix
  27. Subject: putenv(3)
  28. Message-ID: <395@uvicctr.UUCP>
  29. Date: 5 May 88 06:40:52 GMT
  30. Organization: University of Victoria, Victoria B.C. Canada
  31.  
  32. EFTH Minix report #2  - May 1988 -  putenv(3)
  33.  
  34. This is an implementation of putenv(3) that we
  35. wrote for Minix. Please consider this a public
  36. domain program.
  37. */
  38.  
  39. #define NULL 0
  40. #define  PSIZE  sizeof(char *)
  41.  
  42. extern  char  **environ;
  43.  
  44. char  *strchr();
  45. char  *malloc();
  46.  
  47. /****************************************************************/
  48. /*                                */
  49. /*      int                            */
  50. /*    putenv( entry )                        */
  51. /*                                */
  52. /*        The "entry" should follow the form         */
  53. /*        "NAME=VALUE". This routine will search the     */
  54. /*        user environment for "NAME" and replace its     */
  55. /*        value with "VALUE".                */
  56. /*                                */
  57. /*        Note that "entry" is not copied, it is used     */
  58. /*        as the environment entry. This means that it     */
  59. /*        must not be unallocated or otherwise modifed     */
  60. /*        by the caller, unless it is replaced by a     */
  61. /*        subsequent putenv().                */
  62. /*                                */
  63. /*        If the name is not found in the environment,     */
  64. /*        then a new vector of pointers is allocated,     */
  65. /*        "entry" is put at the end and the global     */
  66. /*        variable "environ" is updated.            */
  67. /*                                */
  68. /*        This function normally returns 0, but -1    */
  69. /*        is returned if it can not allocate enough     */
  70. /*        space using malloc(3), or "entry" does not    */
  71. /*        contain a '='.                    */
  72. /*                                */
  73. /****************************************************************/
  74.  
  75.  
  76. int
  77. putenv( entry )
  78.   char *entry;
  79. {
  80.   unsigned length;
  81.   unsigned size;
  82.   char     *temp;
  83.   char     **p;
  84.   char     **new_environ;
  85.  
  86.   /*  Find the length of the "NAME="  */
  87.  
  88.   temp = strchr(entry,'=');
  89.   if ( temp == 0 )
  90.     return( -1 );
  91.  
  92.   length = (unsigned) (temp - entry + 1);
  93.  
  94.  
  95.   /*  Scan through the environment looking for "NAME="  */
  96.  
  97.   for ( p=environ; *p != 0 ; p++ )
  98.     if ( strncmp( entry, *p, length ) == 0 )
  99.       {
  100.       *p = entry;
  101.       return( 0 );
  102.       }
  103.  
  104.  
  105.   /*  The name was not found, build a bigger environment  */
  106.  
  107.   size = p - environ;
  108.  
  109.   new_environ = (char **) malloc( (size+2)*PSIZE );
  110.  
  111.   if ( new_environ == (char **) NULL )
  112.     return( -1 );
  113.  
  114.   memcpy ((char *) new_environ, (const char *) environ, size*PSIZE );
  115.  
  116.   new_environ[size]   = entry;
  117.   new_environ[size+1] = NULL;
  118.  
  119.   environ = new_environ;
  120.  
  121.   return(0);
  122. }
  123.  
  124. #endif /* not HAVE_PUTENV */
  125.